package com.servlet.form;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.listener.ContextListener;
import com.servlet.model.database.BaseAcces;
import com.servlet.security.MD5;

public class AdminLoginForm extends HttpServlet
{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		boolean correct = false;
		
		HttpSession session = request.getSession();
		//BaseAcces base = (BaseAcces) this.config.getServletContext().getAttribute("base");
		
		BaseAcces base = ContextListener.base;

		String motDePasse = request.getParameter("motDePasse");
		
		String resultat = null;

		if( motDePasse != null && motDePasse != "")
		{
			motDePasse = MD5.generateCode(motDePasse);
			if( base.verifyAdmin( motDePasse ) )
			{
				correct = true;
			}
			else
			{
				resultat = "mot de passe erroné";
			}
		}
		else
		{
			resultat = "vous devez entrer le mot de passe administrateur";
		}
		
		if( correct )
		{
			System.out.println("Ouverture du compte administrateur : " + motDePasse );
			
			session.setAttribute("motDePasse", motDePasse );
			
			response.sendRedirect("admin");
		}
		else
		{
			System.out.println(resultat);
			
			session.setAttribute("resultat", "La connexion a échoué : " + resultat );
			
			response.sendRedirect("adminLogin");
		}
	}
}
